#pragma once

#ifndef __CONNECTION_MANAGER_H__
#define __CONNECTION_MANAGER_H__

#include "Common.h"
#include "LogManager.h"
#include "TcpHelper.h"

enum Status
{
	// Undefined status.
	None = 0,

	// Authorization was accepted.
	Accepted = 1,

	// User was not found.
	UserNotFound = 2,

	// Key was invalid.
	InvalidKey = 3,

	// Ip was invalid.
	InvalidIp = 4,

	// Version was invalid.
	InvalidVersion = -1
};

typedef struct
{
	string UserName;
	string UserKey;
} ConnectionOptions;

typedef struct
{
	string Address;
	int Port;
	string RequiredVersion;
	string UpdateAddress;
	string ClientAddress;
} ConnectionInformations;

typedef struct
{
	Status Status;
	string NewKey;
	double Duration;
} Authorization;

// Helper for connection operations.
class ConnectionManager
{
public:
	// Construct a ConnectionManager with the given options.
	ConnectionManager(ConnectionOptions& connectionOptions);

	// Get the connection information.
	bool GetConnectionInformations(ConnectionInformations& connectionInformations);

	// Get the authorization.
	bool GetAuthorization(ConnectionInformations& connectionInformations, Authorization& authorization);

private:
	ConnectionOptions m_connectionOptions;

	// Send the handshake.
	bool SendHandShake(TcpHelper& tcpHelper);

	// Read the random number.
	bool ReadRandomNumber(TcpHelper& tcpHelper, unsigned int& randomNumber);

	// Send the informations.
	bool SendInformations(TcpHelper& tcpHelper, const string& clientAddress, const unsigned int& randomNumber);

	// Read the authorization.
	bool ReadAuthorization(TcpHelper& tcpHelper, Authorization& authorization);

	// Check the packet Id.
	bool CheckPacketId(TcpHelper& tcpHelper, TCHAR packetId);
};

#endif  // __CONNECTION_MANAGER_H__